package com.example.fms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fms.dto.req.StandardFreightReqDto;
import com.example.fms.dto.req.UserReqDto;
import com.example.fms.dto.resp.StandardFreightRespDto;
import com.example.fms.dto.resp.UserRespDto;
import com.example.fms.entity.User;
import com.example.fms.service.StandardFreightRuleService;
import com.example.fms.service.UserService;
import com.example.fms.vo.ResponseBean;
import org.apache.commons.collections.BagUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;

@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private UserService userService;


    @GetMapping()
    public ResponseBean<Page<UserRespDto>> getStandardFreightList(@RequestParam(value = "query", required = false) String query ,
                                                                  @RequestParam(value = "pagenum", defaultValue = "1") Integer pagenum,
                                                                  @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        UserReqDto userReqDto = new UserReqDto();
        userReqDto.setPageNum(pagenum);
        userReqDto.setPageSize(pagesize);
        userReqDto.setName(query);
        return ResponseBean.successMeat(userService.getUserList(userReqDto));
    }

    /**
     * 新增
     * @param req 参数
     * @return 结果
     */
    @PostMapping()
    public ResponseBean<Boolean> save(@RequestBody UserReqDto req) throws InvocationTargetException, IllegalAccessException {
        return ResponseBean.successMeat(userService.save(req));
    }

    /**
     * 修改用户状态
     */
    @PutMapping("/{uId}/state/{type}")
    public ResponseBean<User> putType(@PathVariable Boolean type, @PathVariable String uId) {
        return ResponseBean.successMeat(userService.putType(uId,type));
    }


    /**
     * 根据id修改
     */
    @PutMapping("/{id}")
    public ResponseBean<User> putUser(@PathVariable Long id, @RequestBody UserReqDto  req) {
        return ResponseBean.successMeat(userService.putUser(id,req));
    }

    /**
     * 根据id查询客户信息
     */
    @GetMapping("/{id}")
    public ResponseBean<UserRespDto> getUserById(@PathVariable Long id) {
        return ResponseBean.successMeat(userService.getUserById(id));
    }


    /**
     * 根据id删除客户信息
     */
    @DeleteMapping("/{id}")
    public ResponseBean<Boolean> deleteUserById(@PathVariable Long id) {
        return ResponseBean.successMeat(userService.deleteUserById(id));
    }


    /**
     * 分配用户角色
     */
    @PutMapping("/{id}/role")
    public ResponseBean<Boolean> deleteUserById(@PathVariable Long id,@RequestBody UserReqDto  req) {
        return ResponseBean.successMeat(userService.updateRoleByid(id,req));
    }

}
